package com.sky.service.admin.impl;

import com.sky.constant.StatusConstant;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.mapper.user.OrderMapper;
import com.sky.mapper.user.UserMapper;
import com.sky.pojo.Orders;
import com.sky.service.admin.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;




    /**
     * 根据时间段统计营业数据
     * @param begin
     * @param end
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDate begin, LocalDate end) {
        List<String> dateList = new ArrayList<>();
        dateList.add(begin.toString());

        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */

        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //查询总订单数

        int totalOrderCount = orderMapper.selectOrdersByDateAndStatus(begin.toString(),end.toString(),null);

        map.put("status", Orders.COMPLETE_ORDER);
        //营业额

        Map<String, Object> map1 = orderMapper.selectSalesVolumesByDay3(begin.toString(), end.toString(), dateList);
        Double turnover =Double.parseDouble( map1.get(begin.toString()).toString());

        turnover = turnover == null? 0.0 : turnover;

        //有效订单数
        Integer validOrderCount = orderMapper.selectOrdersByDateAndStatus(begin.toString(),end.toString(),Orders.COMPLETE_ORDER);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增用户数

        Map<String, Object> newUsersMap = userMapper.selectEveryDayNewUserTotalByDate(begin.toString(), end.toString(), dateList);
        Integer newUsers = Integer.parseInt(newUsersMap.get(begin.toString()).toString());



        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }


    /**
     * 查询订单管理数据
     *
     * @return
     */
    public OrderOverViewVO getOrderOverView() {
        LocalDate begin = LocalDate.now();
        LocalDate end = LocalDate.now();




        List<String> dateList = new ArrayList<>();
        dateList.add(begin.toString());

        //待接单

        Integer waitingOrders = orderMapper.selectOrdersByDateAndStatus(begin.toString(),end.toString(),Orders.TO_BE_CONFIRMED);

        //待派送

        Integer deliveredOrders = orderMapper.selectOrdersByDateAndStatus(begin.toString(),end.toString(),Orders.CONFIRMED);

        //已完成

        Integer completedOrders = orderMapper.selectOrdersByDateAndStatus(begin.toString(),end.toString(),Orders.COMPLETE_ORDER);


        //已取消

        Integer cancelledOrders = orderMapper.selectOrdersByDateAndStatus(begin.toString(),end.toString(),Orders.CANCELLED_ORDER);


        //全部订单

        Integer allOrders = orderMapper.selectOrdersByDateAndStatus(begin.toString(),end.toString(),null);


        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}